package com.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.common.Result;
import com.car.entity.Vehicle;
import com.car.service.VehicleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆控制器
 *
 * @author Car System
 */
@Tag(name = "车辆管理")
@RestController
@RequestMapping("/vehicle")
@RequiredArgsConstructor
public class VehicleController {

    private final VehicleService vehicleService;

    @Operation(summary = "分页查询车辆")
    @GetMapping("/page")
    public Result<IPage<Vehicle>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        IPage<Vehicle> page = vehicleService.page(current, size, keyword);
        return Result.success(page);
    }

    @Operation(summary = "根据客户ID查询车辆列表")
    @GetMapping("/customer/{customerId}")
    public Result<List<Vehicle>> listByCustomerId(@PathVariable Long customerId) {
        List<Vehicle> list = vehicleService.listByCustomerId(customerId);
        return Result.success(list);
    }

    @Operation(summary = "根据ID查询车辆")
    @GetMapping("/{id}")
    public Result<Vehicle> getById(@PathVariable Long id) {
        Vehicle vehicle = vehicleService.getById(id);
        return Result.success(vehicle);
    }

    @Operation(summary = "新增车辆")
    @PostMapping
    public Result<Void> save(@RequestBody Vehicle vehicle) {
        vehicleService.save(vehicle);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新车辆")
    @PutMapping
    public Result<Void> update(@RequestBody Vehicle vehicle) {
        vehicleService.update(vehicle);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除车辆")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        vehicleService.delete(id);
        return Result.success("删除成功");
    }
}

